package edu.gdpu.memory.stack;

/**
 * @author mazebin
 * @date 2021年 07月10日 11:34:58
 */
public class VariableThreadSafeTest2 {
    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder();
        builder.append(4);
        builder.append(5);
        builder.append(6);
        new Thread(()->{
            test2(builder);
        }).start();
        System.out.println("--------");
        test2(builder);
    }
    //线程安全
    public static  void test1(){
        StringBuilder sb = new StringBuilder();
        sb.append(1);
        sb.append(2);
        sb.append(3);
        System.out.println(sb.toString());

    }
    //非线程安全(变量逃离方法作用范围)
    public static void test2(StringBuilder builder){
        builder.append(1);
        builder.append(2);
        builder.append(3);
        System.out.println(builder.toString());
    }
}
